package com.borderbears.backend.gui.dockables;

import java.awt.CardLayout;
import java.awt.Component;

import javax.swing.JPanel;

import com.borderbears.backend.gui.viewers.AbstractViewer;
import com.borderbears.backend.gui.viewers.ImageViewer;
import com.borderbears.backend.gui.viewers.XMLViewer;
import com.borderbears.backend.media.MediaNode;
import com.borderbears.backend.media.MediaType;
import com.vlsolutions.swing.docking.DockKey;
import com.vlsolutions.swing.docking.Dockable;

public class ItemViewerPanel extends JPanel implements Dockable {

	private final DockKey dockKey;

	private AbstractViewer currentViewer;

	private final XMLViewer xmlViewer;
	private final String VIEWER_EMPTY = "emptyViewer";
	
	private final ImageViewer imageViewer;
	private final String VIEWER_IMAGE = "viewerImage";
	
	private final CardLayout layout;
	
	public ItemViewerPanel(final DockKey key) {
		dockKey = key;

		layout = new CardLayout();
		setLayout(layout);

		imageViewer = new ImageViewer();
		add(imageViewer, VIEWER_IMAGE);
		
		xmlViewer = new XMLViewer();
		add(xmlViewer, VIEWER_EMPTY);

		currentViewer = imageViewer;
	}

	public void showNode(final MediaNode n) {
		if (MediaType.IMAGE == n.getMediaType()) {
			layout.show(this, VIEWER_IMAGE);
			currentViewer = imageViewer;
		} else {
			currentViewer = xmlViewer;
			layout.show(this, VIEWER_EMPTY);
		}
		currentViewer.showNode(n);
	}


	public Component getComponent() {
		return this;
	}

	public DockKey getDockKey() {
		return dockKey;
	}

}
